package de.bbl.android.tools;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import de.bbl.android.classes.CompletePlaydays;
import de.bbl.android.classes.PlayDay;

public class XmlParseGamePlanTools {

	public static CompletePlaydays XmlParseGameDay(String xmlFilename) {
    	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    	try {
    		DocumentBuilder db = dbf.newDocumentBuilder();
    		Document dom = db.parse(new File(xmlFilename));
    		Element docElement = dom.getDocumentElement();
    		NodeList nl = docElement.getElementsByTagName("key");
    		int postitionOfMatches = -1;
    		Node array = null;
    		CompletePlaydays retVal = new CompletePlaydays();
			String compID = "";
			String compStr = "";
    		
    		for (int i = 0; i < nl.getLength(); i++) {
    			String tempTextValue = nl.item(i).getTextContent().toString();
    			PlayDay[] tempPD = null;
    			if (tempTextValue.equals("competitionID")) {
    				array = nl.item(i).getNextSibling();
    				compID = getNextValue(nl.item(i));
    			}
    			
    			if (tempTextValue.equals("competitionName")) {
    				array = nl.item(i).getNextSibling();
    				compStr = getNextValue(nl.item(i));
    			}
    			
    			if (tempTextValue.equals("matches")) {    				
    				postitionOfMatches = i;
    				array = nl.item(i).getNextSibling();
    				while (array != null && !array.getNodeName().equals("array"))
    					array = array.getNextSibling();
    				
    				if (postitionOfMatches != -1)
    					tempPD = XmlParsePlaydays(array);
    				
    				if (compID.equals("1")) {
    					retVal.addCompString(compStr);
    					retVal.setLeagueGames(tempPD);
    				}
    				else if (compID.equals("2")) {
    					retVal.addCompString(compStr);
    					retVal.setPlayoffQuarterFinal(tempPD);
    				}
    				else if (compID.equals("3")) {
    					retVal.addCompString(compStr);
    					retVal.setPlayoffSemiFinal(tempPD);
    				}
    				else if (compID.equals("4")) {
    					retVal.addCompString(compStr);
    					retVal.setPlayoffFinal(tempPD);
    				}
    			}
    		}
  			return retVal;
    		
    		
    	} catch (Exception e) {
        	Tools.AppendToErrorFile(e.getMessage());
            // e.printStackTrace();
    	}
		return null;
	}
	
	public static PlayDay[] XmlParsePlaydays(Node arrayNode) {
		
		ArrayList<PlayDay> stringList = new ArrayList<PlayDay>();
	    NodeList nl = ((Element) arrayNode).getElementsByTagName("dict");
	    for (int i = 0; i < nl.getLength(); i++) {
	    	if (nl.item(i).getParentNode().getNodeName().equals("array")) {
	    		stringList.add(XmlParseSinglePlayday(nl.item(i)));
	    	}
	    }
	    PlayDay[] retVal = new PlayDay[stringList.size()];
	    stringList.toArray(retVal);
		return retVal;
	}
	
	public static PlayDay XmlParseSinglePlayday(Node matchNode) {
		String matchDayNumber = "";
		String kickoffFirst = "";
		String kickoffLast ="";
		
		// hier sind wir im ArrayNode
		int childNodeCount = matchNode.getChildNodes().getLength();
		
		for (int i = 0; i < childNodeCount; i++) {
			Node currentChildNode = matchNode.getChildNodes().item(i);
			
			if (checkKey(currentChildNode, "matchdayNumber")){
				matchDayNumber = getNextValue(currentChildNode);
			}
			if (checkKey(currentChildNode, "kickoffFirst")){
				kickoffFirst = getNextValue(currentChildNode);
			}
			if (checkKey(currentChildNode, "kickoffLast")){
				kickoffLast = getNextValue(currentChildNode);
			}
		}
		PlayDay retValPlayDay = new PlayDay(matchDayNumber, kickoffFirst, kickoffLast);
		return retValPlayDay;
	}
	
	public static boolean checkKey(Node node,String match) {
		return node.getNodeName().equals("key") && node.getTextContent().equals(match);		
	}
	
	public static String getNextValue(Node currentNode) {
		String retval = "";
		currentNode = currentNode.getNextSibling();
		currentNode = currentNode.getNextSibling();
		if (currentNode.getNodeName().equals("integer") || currentNode.getNodeName().equals("string") || currentNode.getNodeName().equals("date")) {
			retval = currentNode.getTextContent();
		}				 
		return retval;
	}

}
